package com.byd.android.start;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.byd.android.MusicPlayer.MusicPlayer;
import com.byd.android.QRcode.QrCode;
import com.byd.android.VideoPlayer.VideoPlayer;
import com.byd.android.mediaplayer.BaseActivity;
import com.byd.android.mediaplayer.R;

import static com.byd.android.QRcode.QrCode.isApOn;
import static com.byd.android.agreement.InstructionList.MODE.MODE_MUSIC;
import static com.byd.android.agreement.InstructionList.MODE.MODE_VIDEO;

public class MainInterface extends BaseActivity implements View.OnClickListener {
    public static final String TAG = "MainInterface";
    private TextView musicTv;
    private TextView videoTv;
    private ImageView qrCodeIv;
    Intent intent;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case MODE_MUSIC:
                    initMediaMode(MusicPlayer.class);
                    break;
                case MODE_VIDEO:
                    initMediaMode(VideoPlayer.class);

                    //添加Video启动
                    break;
                    default:
                        break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_main);
        init();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.music_interface:
               initMediaMode(MusicPlayer.class);
               mServiceBinder.getMode(MODE_MUSIC);                 //通知服务，模式切换为音乐
                break;
            case R.id.video_interface:
                //此处进行打开视频播放主界面的intent
                //此处把模式命令传往MediaService
                initMediaMode(VideoPlayer.class);
                mServiceBinder.getMode(MODE_VIDEO);
           //     mServiceBinder.getMode(MODE_VIDEO);                //模式切换为视频，不需要通知
                break;
            case R.id.qrcode_acv:
                if(isApOn(this)){
                    initMediaMode(QrCode.class);
                }else {
                    Toast.makeText(this, "热点未开启", Toast.LENGTH_SHORT).show();
                }

            default:
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart:  is done");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop:  is done");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: is done ");

    }

    private void confirmPermission() {
        if (ContextCompat.checkSelfPermission(MainInterface.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainInterface.this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            Log.d("init", "is ready");
            //  getSong();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //   initMediaPlayer();
                } else {
                    Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
                break;
        }
    }
    public void initMediaMode(Class<?> cls){
        intent = new Intent(MainInterface.this, cls);
        startActivity(intent);
    }
    public void init() {
        confirmPermission();
        musicTv = (TextView) findViewById(R.id.music_interface);
        videoTv = (TextView) findViewById(R.id.video_interface);
        qrCodeIv = (ImageView) findViewById(R.id.qrcode_acv);
        qrCodeIv.setImageDrawable(getResources().getDrawable(R.drawable.ic_twobarcode, null));

        musicTv.setOnClickListener(this);
        videoTv.setOnClickListener(this);
        qrCodeIv.setOnClickListener(this);
//        intent = new Intent(MainInterface.this, MediaService.class);
//        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void sendHandlerToService() {
        mServiceBinder.sendHandler(handler);
    }
}
